package com.sdehualu.yinfo.service;


import com.sdehualu.yinfo.dao.SxxxUserDao;
import com.sdehualu.yinfo.dao.kkxxUserDao;
import com.sdehualu.yinfo.entity.KkxxKey;
import com.sdehualu.yinfo.entity.KkxxUser;
import com.sdehualu.yinfo.entity.SxxxKey;
import com.sdehualu.yinfo.entity.SxxxUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private kkxxUserDao kkxxuserdao;


    @Autowired
    private SxxxUserDao sxxxUserDao;


    //构建PageRequest,用于分页
    private PageRequest buildPageRequest(Integer pageNumber, Integer pagzSize, String sortProperty,
                                         String sortDirection) {
        Sort sort;
        if (sortDirection == "ASC") {
            sort = new Sort(Sort.Direction.ASC, sortProperty);
        } else {
            sort = new Sort(Sort.Direction.DESC, sortProperty);
        }
        return new PageRequest(pageNumber - 1, pagzSize, sort);
    }


    private Specification<KkxxUser> buildSpecification(final Map<String, String> searchParams) {
        Specification<KkxxUser> spec = new Specification<KkxxUser>() {
            @Override
            public Predicate toPredicate(Root<KkxxUser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();

                String kkbhlike = searchParams.get("kkbh");

                String kkmclike = searchParams.get("kkmc");
                String keylike = searchParams.get("key");


                if (searchParams.get("key") != null && searchParams.get("key").trim().length() > 0) {
                    list.add(criteriaBuilder.like(root.get("kkbh"), keylike + "%"));

                }


                if (searchParams.get("kkbh") != null && searchParams.get("kkbh").trim().length() > 0) {
                    list.add(criteriaBuilder.like(root.get("kkbh"), "%" + kkbhlike + "%"));

                }

                if (searchParams.get("kklx") != null && searchParams.get("kklx").trim().length() > 0) {
                    list.add(criteriaBuilder.equal(root.get("kklx"), searchParams.get("kklx")));

                }
                if (searchParams.get("xzqh") != null && searchParams.get("xzqh").trim().length() > 0) {
                    list.add(criteriaBuilder.equal(root.get("xzqh"), searchParams.get("xzqh")));

                }
                if (searchParams.get("kkmc") != null && searchParams.get("kkmc").trim().length() > 0) {
                    list.add(criteriaBuilder.like(root.get("kkmc"), "%" + kkmclike + "%"));

                }


                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }

        };

        return spec;
    }


    private Specification<SxxxUser> buildsxSpecification(final Map<String, String> searchParams) {
        Specification<SxxxUser> spec = new Specification<SxxxUser>() {
            @Override
            public Predicate toPredicate(Root<SxxxUser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                String sxjbhlike = searchParams.get("sxjbh");
                String sxjmclike = searchParams.get("sxjmc");

                String keylike = searchParams.get("key");

                if (searchParams.get("key") != null && searchParams.get("key").trim().length() > 0) {
                    list.add(criteriaBuilder.like(root.get("sxjbh"), keylike + "%"));

                }


                if (searchParams.get("sxjbh") != null && searchParams.get("sxjbh").trim().length() > 0) {
                    list.add(criteriaBuilder.like(root.get("sxjbh"), "%" + sxjbhlike + "%"));

                }

                if (searchParams.get("dwxh") != null && searchParams.get("dwxh").trim().length() > 0) {
                    list.add(criteriaBuilder.equal(root.get("dwxh"), searchParams.get("dwxh")));

                }
                if (searchParams.get("sxjmc") != null && searchParams.get("sxjmc").trim().length() > 0) {
                    list.add(criteriaBuilder.like(root.get("sxjmc"), "%" + sxjmclike + "%"));

                }


                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }

        };

        return spec;
    }


    private Specification<KkxxUser> buildjzSpecification(final Map<String, String> searchParams) {
        Specification<KkxxUser> spec = new Specification<KkxxUser>() {
            @Override
            public Predicate toPredicate(Root<KkxxUser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();


                if (searchParams.get("kkbh") != null && searchParams.get("kkbh").trim().length() > 0) {
                    list.add(criteriaBuilder.equal(root.get("kkbh"), searchParams.get("kkbh")));

                }

                if (searchParams.get("kklx") != null && searchParams.get("kklx").trim().length() > 0) {
                    list.add(criteriaBuilder.equal(root.get("kklx"), searchParams.get("kklx")));

                }
                if (searchParams.get("xzqh") != null && searchParams.get("xzqh").trim().length() > 0) {
                    list.add(criteriaBuilder.equal(root.get("xzqh"), searchParams.get("xzqh")));

                }
                if (searchParams.get("kkmc") != null && searchParams.get("kkmc").trim().length() > 0) {
                    list.add(criteriaBuilder.equal(root.get("kkmc"), searchParams.get("kkmc")));

                }


                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }

        };

        return spec;
    }


    private Specification<SxxxUser> buildjzsxSpecification(final Map<String, String> searchParams) {
        Specification<SxxxUser> spec = new Specification<SxxxUser>() {
            @Override
            public Predicate toPredicate(Root<SxxxUser> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<Predicate>();
                String sxjbhlike = searchParams.get("sxjbh");


                if (searchParams.get("sxjbh") != null && searchParams.get("sxjbh").trim().length() > 0) {
                    list.add(criteriaBuilder.equal(root.get("sxjbh"), searchParams.get("sxjbh")));

                }

                if (searchParams.get("dwxh") != null && searchParams.get("dwxh").trim().length() > 0) {
                    list.add(criteriaBuilder.equal(root.get("dwxh"), searchParams.get("dwxh")));

                }
                if (searchParams.get("sxjmc") != null && searchParams.get("sxjmc").trim().length() > 0) {
                    list.add(criteriaBuilder.equal(root.get("sxjmc"), searchParams.get("sxjmc")));

                }


                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }

        };

        return spec;
    }


    @Override
    public boolean isExist(String kkxxKey) {
        return kkxxuserdao.existsById(kkxxKey);
    }

    @Override
    public String addkkxxinfo(KkxxUser kkxxu) {


        try {
            kkxxuserdao.saveAndFlush(kkxxu);
        } catch (Exception e) {
            System.out.println("*****addkkxxinfo 捕获到异常******");
            System.out.println("插入数据异常，请重试");

            e.printStackTrace();
            return "插入数据异常，请重试";
        }


//        kkxxuserdao.save(kkxxu)

        return "success";
    }


    @Override
    public String delkkxxinfo(String kkxxKey) {

        try {
            kkxxuserdao.deleteById(kkxxKey);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("*****delkkxxinfo 捕获到异常******");
            System.out.println("删除失败，请重试");

            return "删除失败，请重试";
        }


        return "success";
    }

    @Override
    public List<KkxxUser> selallinfo() {
        return kkxxuserdao.findAll();

//        return null;
    }

    @Override
    public KkxxUser selinfobk(String kkxxKey) {
        return kkxxuserdao.findById(kkxxKey).orElse(null);
    }

    @Override
    public Page<KkxxUser> findByAnyone(Integer pageNumber, Integer pageSize, String sortProperty, String sortDirection, Map<String, String> searchParams) {
        PageRequest request = this.buildPageRequest(pageNumber, pageSize, sortProperty, sortDirection);
        Specification<KkxxUser> spec = this.buildSpecification(searchParams);
        return kkxxuserdao.findAll(spec, request);
    }

    @Override
    public Page<KkxxUser> findByAnyonejz(Integer pageNumber, Integer pageSize, String sortProperty, String sortDirection, Map<String, String> searchParams) {
        PageRequest request = this.buildPageRequest(pageNumber, pageSize, sortProperty, sortDirection);
        Specification<KkxxUser> spec = this.buildjzSpecification(searchParams);
        return kkxxuserdao.findAll(spec, request);
    }

    @Override
    public String updinfo(KkxxUser kkxxu) {


        return null;
    }


    @Override
    public boolean isExtsx(String sxxxKey) {

//        return kkxxuserdao.existsById(kkbh);
        return sxxxUserDao.existsById(sxxxKey);
    }

    @Override
    public String addsxxxinfo(SxxxUser sxxxUser) {

        try {
            sxxxUserDao.saveAndFlush(sxxxUser);
        } catch (Exception e) {
            System.out.println("*****addsxxxinfo 捕获到异常******");
            System.out.println("插入数据异常，请重试");

            e.printStackTrace();
            return "插入数据异常，请重试";
        }
        return "success";
    }

    @Override
    public String delsxxxinfo(String sxxxKey) {

        try {
            sxxxUserDao.deleteById(sxxxKey);
        } catch (Exception e) {
            System.out.println("*****delsxxxinfo 捕获到异常******");
            System.out.println("删除失败，请重试");

            e.printStackTrace();
            return "删除失败，请重试";
        }
        return "success";
    }

    @Override
    public List<SxxxUser> selallsxinfo() {


        return sxxxUserDao.findAll();
//        return null;
    }

    @Override
    public SxxxUser selinfosxbk(String sxxxKey) {
        return sxxxUserDao.findById(sxxxKey).orElse(null);

    }

    @Override
    public Page<SxxxUser> findsxByAnyone(Integer pageNumber, Integer pageSize, String sortProperty, String sortDirection, Map<String, String> searchParams) {

        PageRequest request = this.buildPageRequest(pageNumber, pageSize, sortProperty, sortDirection);
//        Specification<KkxxUser> spec = this.buildSpecification(searchParams);
        Specification<SxxxUser> specsx = this.buildsxSpecification(searchParams);
        return sxxxUserDao.findAll(specsx, request);
    }

    @Override
    public Page<SxxxUser> findsxByAnyonejz(Integer pageNumber, Integer pageSize, String sortProperty, String sortDirection, Map<String, String> searchParams) {

        PageRequest request = this.buildPageRequest(pageNumber, pageSize, sortProperty, sortDirection);
//        Specification<KkxxUser> spec = this.buildSpecification(searchParams);
        Specification<SxxxUser> specsx = this.buildjzsxSpecification(searchParams);
        return sxxxUserDao.findAll(specsx, request);


    }

    @Override
    public String updsxinfo(SxxxUser sxxxUser) {
        return null;
    }


}
